
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for see_all_members.
	''' </summary>
	Public Partial Class see_all_members
		Inherits BaseWebPage

		Protected txtStartIndexHidden As System.Web.UI.WebControls.TextBox
		Protected lblInterests As System.Web.UI.WebControls.Label
		Protected blankImagesCount As Integer = 0
		Protected currentPageNumber As Integer = 0
		Protected firstPageNumber As Integer = 0
		' the first page of page links
		Protected lastPageNumber As Integer = 0
		Protected pagesRest As Integer = 0
		Protected action As String = "0"
		Protected usrID As Integer = -1
		Protected grpID As Integer = -1
		Protected grp As Group
		Protected usr As User
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.SEE_ALL_MEMBERS_PAGE;
			usr = LoggedUser

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.SEE_ALL_MEMBERS, "", _Form1, Nothing, htPageConstants)
			Me.initAdvertisements(_Form1, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If Request.Params(Constants.GROUP_PARAM) = Nothing OrElse Request.Params(Constants.GROUP_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			grp = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
			End If

			' init members
			dlAllMembers.RepeatColumns = Constants.MAX_PICTURES_ON_ROW

			hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
			hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)

			' set add/edit members visiblity
			If usr.ID <> grp.Uid Then
				' if LoggedUser is not the group moderator
				imgAddFriends.Visible = False
				lbAddMembers.Visible = False
				imgEditFriends.Visible = False
				lbEditMembers.Visible = False
			End If

			Dim MembersCount As Double = grp.Members.Count

			litGroupName.Text = grp.Name
			litMembersCount.Text = MembersCount.ToString()

			' NOTE: The check if the members are 0 i removed because a group will have at least one member - group's moderator

			dgPager.RedirectPage = Constants.SEE_ALL_MEMBERS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
			dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(MembersCount / Constants.MAX_PICTURES_ON_PAGE))
			dgPagerUp.RedirectPage = Constants.SEE_ALL_MEMBERS + "?" + Constants.GROUP_PARAM + "=" + grp.ID
			dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(MembersCount / Constants.MAX_PICTURES_ON_PAGE))
			action = Request.Params("action")
			dgPager.Action = action
			dgPagerUp.Action = action
			If action = Constants.SEE_ALL_FRIENDS_EDIT_ACTION Then
				imgEditFriends.Visible = False
				lbEditMembers.Visible = False
			End If

			' initialize listing literals

			dgPager.dgListing.TotalCount = Convert.ToInt32(MembersCount)
			dgPagerUp.dgListing.TotalCount = Convert.ToInt32(MembersCount)

			'initialize current page number
			If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
				dgPager.CurrentPageNumber = 1
				dgPagerUp.CurrentPageNumber = 1
				ViewState("StartIndex") = 0
			Else
				dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
				dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
				ViewState("StartIndex") = Convert.ToString(((dgPager.CurrentPageNumber - 1) * Constants.MAX_PICTURES_ON_PAGE))
			End If

			dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
			dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
			dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
			dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

			If Not IsPostBack Then
				BindList()
			End If
		End Sub

		Public Function CreateDataSource() As ICollection
			Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
			Dim endIndex As Integer = startIndex + Constants.MAX_PICTURES_ON_PAGE
			Dim i As Integer
			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("DelMemberVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("MemberID", GetType(String)))
			dt.Columns.Add(New DataColumn("FullName", GetType(String)))
			Dim temp As UserPicture
			Dim MembersCount As Integer = grp.Members.Count

			Dim imagesOnLastRow As Integer = 0
			Dim t As Integer = 0
			Dim counter As Integer = 0

			endIndex = Math.Min(endIndex, MembersCount)
			t = (endIndex - startIndex)
			Math.DivRem(t, Constants.MAX_PICTURES_ON_ROW, imagesOnLastRow)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			counter = 0
			i = 0

			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			For Each gm As BO.GroupMember In grp.Members
				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()
					temp = gm.User.DefaultPicture

					If temp <> Nothing Then
						dr(0) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(0) = "../images/default_thumbnail.gif"
					End If
					dr(1) = getBeginning(HttpUtility.HtmlEncode(gm.User.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					If LoggedUsersList.Contains(gm.User.ID) Then
						dr(2) = "../images/online_status.gif"
					Else
						dr(2) = "../images/spacer.gif"
					End If

					dr(3) = False
					If action = Constants.SEE_ALL_FRIENDS_EDIT_ACTION Then
						If grp.Uid <> gm.User.ID Then
							dr(3) = True
						End If
					End If

					dr(4) = gm.User.ID
					dr(5) = gm.User.Name
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next

			If imagesOnLastRow <> 0 Then
				blankImagesCount = Constants.MAX_PICTURES_ON_ROW - imagesOnLastRow
				i = imagesOnLastRow
				While i < Constants.MAX_PICTURES_ON_ROW
					dr = dt.NewRow()
					'dr[0] = "../images/empty_image.gif";
					dr(0) = "../images/spacer.gif"
					dr(1) = ""
					dr(2) = "../images/spacer.gif"
					dr(3) = "false"
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Public Sub BindList()
			dlAllMembers.DataSource = CreateDataSource()
			dlAllMembers.DataBind()

			Dim itemsCount As Integer = dlAllMembers.Items.Count
			Dim blankImageIndex As Integer = itemsCount - blankImagesCount
			Dim i As Integer = 0
			i = blankImageIndex
			While i < itemsCount
				(DirectCast(dlAllMembers.Items(i).FindControl("lbName"), System.Web.UI.WebControls.LinkButton)).CssClass = "empty_image"
				(DirectCast(dlAllMembers.Items(i).FindControl("imgPhoto"), System.Web.UI.WebControls.ImageButton)).CssClass = "empty_image"
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
			Dim stringBuilder As New StringBuilder()
			Dim stringWriter As New StringWriter(stringBuilder)
			Dim htmlWriter As New HtmlTextWriter(stringWriter)
			MyBase.Render(htmlWriter)
			Dim html As String = stringBuilder.ToString()
			Dim start As Integer = html.IndexOf("<form name=""") + 12
			Dim [end] As Integer = html.IndexOf("""", start)
			Dim formID As String = html.Substring(start, [end] - start)
			Dim replace As String = formID.Replace(":", "_")
			html = html.Replace("document." + formID, "document." + replace)
			writer.Write(html)
		End Sub

		Private Sub dlAllMembers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim srcType As String = src.[GetType]().ToString()
			Dim cmd As String = e.CommandName
			Dim arg As String = ""
			If srcType.Equals("System.Web.UI.WebControls.ImageButton") Then
				arg = (DirectCast(src, ImageButton)).CommandArgument
			Else
				arg = (DirectCast(src, LinkButton)).CommandArgument
			End If
			Select Case cmd
				Case "Delete"
					grp.RejectMembership(usr.ID, Convert.ToInt32(arg))
					litMembersCount.Text = grp.Members.Count.ToString()
					BindList()
					Exit Select
				Case "MemberRedirect"
					Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
					Exit Select
			End Select
		End Sub

		Protected Sub lbEditMembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(Constants.SEE_ALL_MEMBERS + "?action=" + Constants.SEE_ALL_FRIENDS_EDIT_ACTION + "&" + Constants.GROUP_PARAM + "=" + grpID)
		End Sub

		Protected Sub lbAddMembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(Constants.SEND_MESSAGE + "?action=" + Constants.MESSAGE_FORWARD_A_GROUP_ACTION + "&" + Constants.FORWARD_A_GROUP_PARAM + "=" + grpID)
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlAllMembers.ItemCommand, AddressOf Me.dlAllMembers_ItemCommand
		End Sub
#End Region

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable _Form1.
		====================
		Public ReadOnly Property _Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated__Form1
			End Get
		End Property
		'set { Migrated__Form1 = value; }
	End Class
End Namespace






